package com.hss01248.dialog.ios;

import com.hss01248.dialog.ResourceTable;
//import com.hss01248.dialog.StyledDialog;
import com.hss01248.dialog.Tool;
import com.hss01248.dialog.adapter.SuperLvHolder;
import com.hss01248.dialog.config.ConfigBean;
import ohos.agp.components.*;
import ohos.agp.components.element.ShapeElement;
import ohos.agp.components.Text.TextSizeType;
import ohos.agp.components.element.StateElement;
import ohos.app.Context;

/**
 * Created by Administrator on 2016/10/9 0009.
 */
public class IosCenterItemHolder extends SuperLvHolder<ConfigBean> {
    public ListContainer lv;
    public Text textView;
    private Component line;

    public IosCenterItemHolder(Context context) {
        super(context);
    }

    @Override
    protected void findViews() {
        lv = (ListContainer) rootView.findComponentById(ResourceTable.Id_lv);
        textView = (Text)rootView.findComponentById(ResourceTable.Id_tv_title);

        line = rootView.findComponentById(ResourceTable.Id_v_line);
    }

    @Override
    protected int setLayoutRes() {
        return ResourceTable.Layout_dialog_ios_center_item;
    }

    @Override
    public void assingDatasAndEvents(final Context context, final ConfigBean bean) {
        if(Tool.isEmpty(bean.title)){
            textView.setVisibility(Component.HIDE);
            line.setVisibility(Component.HIDE);
        }else {
            textView.setVisibility(Component.VISIBLE);
            line.setVisibility(Component.VISIBLE);
            textView.setText(bean.title.toString());
            if(bean.titleTxtSize>0){
                textView.setTextSize(bean.titleTxtSize, TextSizeType.FP);
            }
            if(bean.titleTxtColor !=0){
                Tool.setTextColorById(textView, bean.titleTxtColor);
            }
        }
        lv.setItemProvider(new BaseItemProvider() {
            @Override
            public int getCount() {
                return bean.wordsIos.size();
            }

            @Override
            public Object getItem(int position) {
                return bean.wordsIos.get(position);
            }

            @Override
            public long getItemId(int position) {
                return position;
            }

            @Override
            public Component getComponent(final int position, Component convertView, ComponentContainer parent) {
                LayoutScatter scatter = LayoutScatter.getInstance(context);

                DependentLayout root = (DependentLayout)scatter.parse(ResourceTable.Layout_item_btn_bottomalert, null, false);
                Button view = (Button) root.findComponentById(ResourceTable.Id_btn);

                int xmlId = 0;
                if (getCount() >=2){
                    if (position ==0){
                        if(Tool.isEmpty(bean.title)){
                            xmlId = ResourceTable.Graphic_selector_btn_press_all_top;
                        }else {
                            xmlId = ResourceTable.Graphic_selector_btn_press_no_corner;
                        }

                    }else if (position == getCount() -1){
                        xmlId = ResourceTable.Graphic_selector_btn_press_all_bottom;
                    }else {
                        xmlId = ResourceTable.Graphic_selector_btn_press_no_corner;
                    }

                }else {
                    xmlId = ResourceTable.Graphic_selector_btn_press_all;
                }
                StateElement se = new StateElement();
                se.addState(new int[]{ComponentState.COMPONENT_STATE_PRESSED}, new ShapeElement(context, ResourceTable.Graphic_bg_ios_roundcorner_gray));
                se.addState(new int[]{ComponentState.COMPONENT_STATE_EMPTY}, new ShapeElement(context, ResourceTable.Graphic_bg_ios_roundcorner));
                view.setBackground(se);
                view.setText(bean.wordsIos.get(position).toString());
                view.setClickedListener(new Component.ClickedListener() {
                    @Override
                    public void onClick(Component v) {
                        bean.itemListener.onItemClick(bean.wordsIos.get(position),position);
                        Tool.dismiss(bean,true);

                    }
                });

                return root;
            }
        });
    }


}
